using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SharpPlanets
{
	/// <summary>
	/// Summary description for WinForm.
	/// </summary>
	public class UserInterface : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.TextBox speedBox;
		private System.Windows.Forms.TextBox angleBox;
		private System.Windows.Forms.PictureBox backgroundBox;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
        private System.Windows.Forms.MenuItem menuItem9;
        private Timer timer1;
        private TrackBar trackBar1;
        private TrackBar trackBar2;
		private System.Windows.Forms.MenuItem menuItem10;

		public UserInterface() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UserInterface));
            System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
            this.toolBar1 = new System.Windows.Forms.ToolBar();
            this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.speedBox = new System.Windows.Forms.TextBox();
            this.angleBox = new System.Windows.Forms.TextBox();
            this.backgroundBox = new System.Windows.Forms.PictureBox();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem6 = new System.Windows.Forms.MenuItem();
            this.menuItem7 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem8 = new System.Windows.Forms.MenuItem();
            this.menuItem9 = new System.Windows.Forms.MenuItem();
            this.menuItem10 = new System.Windows.Forms.MenuItem();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            this.trackBar2 = new System.Windows.Forms.TrackBar();
            ((System.ComponentModel.ISupportInitialize)(this.backgroundBox)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit();
            this.SuspendLayout();
            //
            // toolBar1
            //
            this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
            this.toolBarButton1});
            this.toolBar1.DropDownArrows = true;
            this.toolBar1.ImageList = this.imageList1;
            this.toolBar1.Location = new System.Drawing.Point(0, 0);
            this.toolBar1.Name = "toolBar1";
            this.toolBar1.ShowToolTips = true;
            this.toolBar1.Size = new System.Drawing.Size(632, 28);
            this.toolBar1.TabIndex = 0;
            this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
            //
            // toolBarButton1
            //
            this.toolBarButton1.ImageIndex = 0;
            this.toolBarButton1.Name = "toolBarButton1";
            this.toolBarButton1.ToolTipText = "Shoot!";
            //
            // imageList1
            //
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "");
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(72, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(40, 16);
            this.label1.TabIndex = 1;
            this.label1.Text = "speed";
            //
            // label2
            //
            this.label2.Location = new System.Drawing.Point(334, 8);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(40, 16);
            this.label2.TabIndex = 2;
            this.label2.Text = "angle";
            this.label2.Click += new System.EventHandler(this.label2_Click);
            //
            // speedBox
            //
            this.speedBox.Location = new System.Drawing.Point(109, 4);
            this.speedBox.Name = "speedBox";
            this.speedBox.Size = new System.Drawing.Size(40, 20);
            this.speedBox.TabIndex = 3;
            this.speedBox.TextChanged += new System.EventHandler(this.speedBox_TextChanged);
            this.speedBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.speedBox_KeyDown);
            this.speedBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.speedBox_KeyPress);
            //
            // angleBox
            //
            this.angleBox.Location = new System.Drawing.Point(367, 4);
            this.angleBox.Name = "angleBox";
            this.angleBox.Size = new System.Drawing.Size(40, 20);
            this.angleBox.TabIndex = 4;
            this.angleBox.TextChanged += new System.EventHandler(this.angleBox_TextChanged);
            this.angleBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.angleBox_KeyDown);
            this.angleBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.angleBox_KeyPress);
            //
            // backgroundBox
            //
            this.backgroundBox.BackColor = System.Drawing.Color.Black;
            this.backgroundBox.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("backgroundBox.BackgroundImage")));
            this.backgroundBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.backgroundBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.backgroundBox.Location = new System.Drawing.Point(0, 28);
            this.backgroundBox.Name = "backgroundBox";
            this.backgroundBox.Size = new System.Drawing.Size(632, 438);
            this.backgroundBox.TabIndex = 5;
            this.backgroundBox.TabStop = false;
            this.backgroundBox.Resize += new System.EventHandler(this.backgroundBox_Resize);
            this.backgroundBox.Paint += new System.Windows.Forms.PaintEventHandler(this.backgroundBox_Paint);
            //
            // statusBar1
            //
            this.statusBar1.Location = new System.Drawing.Point(0, 444);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(632, 22);
            this.statusBar1.TabIndex = 6;
            this.statusBar1.Text = "statusBar1";
            //
            // mainMenu1
            //
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.menuItem2,
            this.menuItem3});
            //
            // menuItem1
            //
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem4,
            this.menuItem5});
            this.menuItem1.Text = "File";
            //
            // menuItem4
            //
            this.menuItem4.Index = 0;
            this.menuItem4.Text = "Nuovo gioco";
            this.menuItem4.Click += new System.EventHandler(this.newGameClick);
            //
            // menuItem5
            //
            this.menuItem5.Index = 1;
            this.menuItem5.Text = "Esci";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
            //
            // menuItem2
            //
            this.menuItem2.Index = 1;
            this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem6,
            this.menuItem7});
            this.menuItem2.Text = "Azioni";
            //
            // menuItem6
            //
            this.menuItem6.Index = 0;
            this.menuItem6.Text = "Abbandona";
            this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
            //
            // menuItem7
            //
            this.menuItem7.Index = 1;
            this.menuItem7.Text = "Spara";
            this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
            //
            // menuItem3
            //
            this.menuItem3.Index = 2;
            this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem8,
            this.menuItem9,
            this.menuItem10});
            this.menuItem3.Text = "Help";
            //
            // menuItem8
            //
            this.menuItem8.Index = 0;
            this.menuItem8.Text = "Informazioni";
            //
            // menuItem9
            //
            this.menuItem9.Index = 1;
            this.menuItem9.Text = "Guida in linea";
            //
            // menuItem10
            //
            this.menuItem10.Index = 2;
            this.menuItem10.Text = "Sito web";
            //
            // timer1
            //
            this.timer1.Interval = 5;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // trackBar1
            //
            this.trackBar1.Location = new System.Drawing.Point(155, 4);
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(150, 45);
            this.trackBar1.TabIndex = 7;
            //
            // trackBar2
            //
            this.trackBar2.Location = new System.Drawing.Point(413, 4);
            this.trackBar2.Name = "trackBar2";
            this.trackBar2.Size = new System.Drawing.Size(150, 45);
            this.trackBar2.TabIndex = 8;
            //
            // UserInterface
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CausesValidation = ((bool)(configurationAppSettings.GetValue("UserInterface.CausesValidation", typeof(bool))));
            this.ClientSize = new System.Drawing.Size(632, 466);
            this.Controls.Add(this.trackBar2);
            this.Controls.Add(this.trackBar1);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.backgroundBox);
            this.Controls.Add(this.angleBox);
            this.Controls.Add(this.speedBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.toolBar1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = this.mainMenu1;
            this.MinimumSize = new System.Drawing.Size(640, 480);
            this.Name = "UserInterface";
            this.Text = "Sharp Planets";
            this.Load += new System.EventHandler(this.UserInterface_Load);
            ((System.ComponentModel.ISupportInitialize)(this.backgroundBox)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new UserInterface());
		}

        /****************** CAMPI ********************/
        private ArrayList planetList;
        private Rocket rocketTest;
        private LeftShip leftShip;
        private RightShip rightShip;


		/****************** METODI ********************/


        /// <summary>
		///
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			//Planet pianetaX;
			//pianetaX = new Jupiter();
			//pianetaX.myPosition.setXAbs(100);
			//pianetaX.myPosition.setYAbs(200);
		    //textBox1.Text = (terra.myPosition.getXRel(1000)).ToString();
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{

		}

		private void backgroundBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{

			foreach (MassiveSpaceObject iteratore in planetList) {
				iteratore.Paint(e.Graphics);
			}
            if (rocketTest!=null) rocketTest.Paint(e.Graphics);
            if (rightShip != null) rightShip.Paint(e.Graphics);
            if (leftShip != null) leftShip.Paint(e.Graphics);
		}


		private void UserInterface_Load(object sender, System.EventArgs e)
		{
			// create list of pointers
			planetList = new ArrayList();
            rocketTest = new Rocket();
            rocketTest.CenterPosition = new Position(1000,1000);
         }

		private void newGameClick(object sender, System.EventArgs e)
		{
            if (planetList!=null)
            {
                planetList.Clear();
            }
            else
            {
                planetList = new ArrayList();
            }


            //create list of random numbers for radius
            int[] rndradius = new int[3];
            Random randomNumber = new Random();
            rndradius[0] = randomNumber.Next(3000,10000);
            rndradius[1] = randomNumber.Next(3000,10000);
            rndradius[2] = randomNumber.Next(3000,10000);

            //create list of random numbers for yPosition
            int[] rndyPosition = new int[5];
            Random randomNumberY = new Random();
            rndyPosition[0] = randomNumberY.Next(3000, 50000);
            rndyPosition[1] = randomNumberY.Next(3000, 50000);
            rndyPosition[2] = randomNumberY.Next(3000, 50000);
            rndyPosition[3] = randomNumberY.Next(3000, 50000);
            rndyPosition[4] = randomNumberY.Next(3000, 50000);

			// create one by one, each planet
            Star xy = new Aldebaran();
            xy.StarType = StarType.RedGiant;

            Star xyz = new Sun();
            xyz.StarType = StarType.NeutronStar;

            //Planet prova = new Comet();
            //prova.Radius = 0;
            //Asteroid x = new Asteroid();
            //x.Collide(null);
            Jupiter J = new Jupiter(rndradius[0]);
            J.CenterPosition.setXAbs(30000);
            J.CenterPosition.setYAbs(rndyPosition[0]);
            Mercury M = new Mercury(rndradius[1]);
            M.CenterPosition.setXAbs(50000);
            M.CenterPosition.setYAbs(rndyPosition[1]);
            Venus V = new Venus(rndradius[2]);
            V.CenterPosition.setXAbs(70000);
            V.CenterPosition.setYAbs(rndyPosition[2]);
			//adding objects to list
			planetList.Add(J);
			planetList.Add(M);
			planetList.Add(V);

            if (rightShip != null) {
                //nhhh
                rightShip.CenterPosition.setYAbs(rndyPosition[3]);
            }
            else {
                rightShip = new RightShip(new Position(90000, rndyPosition[3]));
            }

            if (leftShip != null) {
                leftShip.CenterPosition.setYAbs(rndyPosition[4]);
            } else {
                leftShip = new LeftShip(new Position(10000, rndyPosition[4]));
            }
            this.Invalidate();
            this.Refresh();
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{

		}

		private void backgroundBox_Resize(object sender, System.EventArgs e)
		{
			this.Invalidate();
            this.Refresh();
		}

        private void timer1_Tick(object sender, EventArgs e)
        {
            Position newPosition = rocketTest.CenterPosition;
            double xdeviation = 0;
            double ydeviation = 0;

            foreach (MassiveSpaceObject iteratore in planetList)
            {
                xdeviation = xdeviation + ((Planet)iteratore).HorizontalDeviation(newPosition);
                ydeviation = ydeviation + ((Planet)iteratore).VerticalDeviation(newPosition);
            }
            newPosition.setXAbs(newPosition.getXAbs() + (int)xdeviation);
            newPosition.setYAbs(newPosition.getYAbs() + (int)ydeviation);
            rocketTest.CenterPosition = newPosition;
            bool Collision = false;
            foreach (MassiveSpaceObject iteratore in planetList)
            {
                if (((Planet)iteratore).Collide(newPosition) )
                    Collision=true;

            }
            if (leftShip.Collide(newPosition) || rightShip.Collide(newPosition))
                Collision = true;

            if (Collision) { timer1.Enabled = false; }
            this.Invalidate();
            this.Refresh();
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            rocketTest.CenterPosition = new Position(1000, 1000);
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {

        }

        private void angleBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {
            }
            else { e.Handled = true; }

        }

        private void angleBox_KeyDown(object sender, KeyEventArgs e)
        {

        }



        private void speedBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void angleBox_TextChanged(object sender, EventArgs e)
        {

        }


        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void speedBox_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void speedBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {
            }
            else { e.Handled = true; }
        }


	}
}
